package com.oedtech.esl.config.timer;

import com.alibaba.fastjson.JSONObject;
import com.oedtech.esl.controller.WebSocketTest;
import com.oedtech.esl.model.SNModel.SNAp;
import com.oedtech.esl.model.SNModel.SNEslStatus;
import com.oedtech.esl.model.esl.Ap;
import com.oedtech.esl.model.esl.EslStatus;
import com.oedtech.esl.model.esl.EslTab;
import com.oedtech.esl.service.eslService.ApService;
import com.oedtech.esl.service.eslService.EslTabService;
import com.oedtech.esl.service.nettyService.DataService;
import com.oedtech.esl.socket.BsChannelMap;
import com.oedtech.esl.utils.HttpUtils;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.net.ConnectException;
import java.util.*;

/**
 * @Author QiuLongYu
 * @Date 2019/7/30
 * 苏宁接口上报定时器
 */
@Component
@Configurable
@EnableScheduling
public class StatusSumitTimer {
    private final static Logger LOG= LoggerFactory.getLogger(StatusSumitTimer.class);
    @Autowired
    private DataService dataService;
    @Autowired
    private ApService apService;
    @Autowired
    private EslTabService eslTabService;
    @Value("${sn.vendor_id}")
    private String vendorId;
    @Value("${sn.firmwarePrefix}")
    private String firmwareFlag;
    @Value("${ap.port}")
    private String apPort;
    @Value("${sn.ap_status_url}")
    private String apStatusUrl;
    @Value("${sn.esl_status_url}")
    private String eslStatusUrl;
    @Value("${sn.report_esl_size}")
    private Integer reportEslSize;
    /**
     * 上报基站状态
     */
    @Scheduled(cron="${sn.ap_cron}")
    public void apStatusTimer(){
        LOG.info("report ap status");
        List<Ap>list= apService.findAllAp();
        List<SNAp> snResult = new ArrayList<>();
        for(Ap ap:list){
            snResult.add(new SNAp(ap, vendorId, apPort));
        }
        JSONObject jo = new JSONObject();
        jo.put("status_no","0");
        jo.put("type","AP_STATUS");
        jo.put("data", snResult);
        String result=HttpUtils.sendPost(apStatusUrl, jo.toJSONString());
        LOG.info("report ap status callback:[{}]",result);
    }


    /**
     * 上报价签状态
     */
        @Scheduled(cron="${sn.esl_cron}")
    public void eslStatusTimer(){
            LOG.info("report esl status");
        List<EslTab> list = eslTabService.findAllEsl();
        List<SNEslStatus> snList = new ArrayList<>();
        JSONObject jo = new JSONObject();

        int n=0;
        for(EslTab eslTab:list){
            n++;
            if(eslTab.getEslStatus()!=null){
                snList.add(new SNEslStatus(eslTab,vendorId,firmwareFlag));
            }


            if(n%reportEslSize==0||n==list.size()){
                jo.put("status_no","0");
                jo.put("type","ESL_HB_STATUS");
                jo.put("data", snList);
                String result=HttpUtils.sendPost(eslStatusUrl,jo.toJSONString());
                LOG.info("report esl status callback:[{}]",result);
                snList.clear();
            }
        }
    }

    private static Map<String, Channel> channelMap = BsChannelMap.getInstance();

    /**
     * 发送p13让基站收集价签状态
     */
        @Scheduled(cron="${sn.p13_cron}")
    public void sendP13(){
            Set<Map.Entry<String, Channel>> entrySet = channelMap.entrySet();
        for(Map.Entry<String,Channel> entry:entrySet){
            dataService.sendP13(entry.getKey(),entry.getValue(),0);
        }
    }

    /**
     * 拆分list，
     * @param list  待拆分list
     * @param len   拆分后的list长度
     * @param <T>   list中的元素类型自定义
     * @return      拆分后的list的list;List<List<T>>
     */
    public static<T> List<List<T>> splitList(List<T> list, int len) {
        if (list == null || list.size() == 0 || len < 1) {
            return null;
        }

        List<List<T>> result = new ArrayList<List<T>>();


        int size = list.size();
        int count = (size + len - 1) / len;


        for (int i = 0; i < count; i++) {
            List<T> subList = list.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
            result.add(subList);
        }
        return result;
    }
}
